package org.jeecg.modules.charge.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.ComUtil;
import org.jeecg.common.util.SsoUtil;
import org.jeecg.modules.charge.entity.ChargeInvoiceInhouse;
import org.jeecg.modules.charge.service.impl.ChargeInvoiceRecordsServiceImpl;
import org.jeecg.vo.charge.ChargeInvoiceInhouseExcel;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @Description: 现有票据统计
* @Author: Lee
* @Date:   2020-03-09
* @Version: V1.0
*/
@Slf4j
@Api(tags="现有票据统计")
@RestController
@RequestMapping("/b/charge/chargeInvoiceReport")
public class ChargeInvoiceReportController {

    @Autowired
    private ChargeInvoiceRecordsServiceImpl chargeInvoiceRecordsListService;

    /**
     * 分页列表查询
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<IPage<ChargeInvoiceInhouse>> list(ChargeInvoiceInhouse inhouse,
                                                      @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                      @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                      HttpServletRequest req) {
        Result<IPage<ChargeInvoiceInhouse>> result = new Result<IPage<ChargeInvoiceInhouse>>();
        try{
            Page<ChargeInvoiceInhouse> pageList = new Page<ChargeInvoiceInhouse>(pageNo, pageSize);
            if(StringUtils.isNotEmpty(inhouse.getId())){
                pageList = chargeInvoiceRecordsListService.getChargeInvoiceReport(pageList,inhouse);
            }
            result.setSuccess(true);
            result.setResult(pageList);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 导出excel
     *
     * @param
     * @param inhouse
     */
    @PostMapping(value = "/exportXls")
    public ModelAndView exportXls(@RequestBody ChargeInvoiceInhouse inhouse) {
        // Step.1 组装查询条件
        Page<ChargeInvoiceInhouse> pageList = new Page<ChargeInvoiceInhouse>(1, Integer.MAX_VALUE);
        LoginUser user = SsoUtil.getLoginUser();
        pageList = chargeInvoiceRecordsListService.getChargeInvoiceReport(pageList,inhouse);
        //Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<ChargeInvoiceInhouseExcel> excelList = ComUtil.copy(pageList.getRecords(),ChargeInvoiceInhouseExcel.class);
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "现有票据统计");
        mv.addObject(NormalExcelConstants.CLASS, ChargeInvoiceInhouseExcel.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("现有票据统计", "导出人:" + user.getRealname(), "导出信息"));
        mv.addObject(NormalExcelConstants.DATA_LIST, excelList);
        return mv;
    }
    /**
       * @param excelName
       *   要生成的文件名字
       * @return
       * @throws IOException
       */
    private ServletOutputStream generateResponseExcel(String excelName, HttpServletResponse response) throws IOException {
        excelName = excelName == null || "".equals(excelName) ? "excel" : URLEncoder.encode(excelName, "UTF-8");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + excelName + ".xlsx");
        return response.getOutputStream();
    }
    private void createExcel(XSSFSheet sheet, int rowNo, String[] vals, XSSFCellStyle style)  {
        if (vals.length > 0) {
            XSSFRow row = sheet.createRow(rowNo);
            for (int i = 0; i < vals.length; i++) {
                String val = vals[i];
                XSSFCell cell = row.createCell(i);
                cell.setCellValue(val);
                cell.setCellStyle(style);
            }
        }
    }

}
